package com.itheima.commons.utils;

import cn.hutool.core.codec.Base64;
import com.baidu.aip.face.AipFace;
import org.json.JSONObject;

public class FaceDetect {

   private static String appid;
    private static String apikey;
    private static String secretkey;

    static {
        appid = ProLoadUtil.proload().getProperty("face.appid");
        apikey = ProLoadUtil.proload().getProperty("face.apikey");
        secretkey = ProLoadUtil.proload().getProperty("face.secretkey");
    }

    public static boolean faceDetect(byte[] imagedata) {
        // 初始化一个AipFace

        AipFace client = new AipFace(appid, apikey, secretkey);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 调用接口
        String image = Base64.encode(imagedata);

        // 人脸检测
        JSONObject res = client.detect(image, "BASE64", null);
        System.out.println(res.toString(2));
        return res.getString("error_msg").equalsIgnoreCase("SUCCESS");
    }
}
